import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
// hack router push callback
// [解决 vue-router跳转相同路径报错 ]
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

export const constantRoutes = [
  {
    name:'login',
    path: '/user/login',
    component: () => import('@/views/user/Login'),
    hidden: true,
    meta: { title: '运营平台', icon: 'el-icon-s-home' }
  },
  {
    name:'404',
    path: '/404',
    component: () => import('@/views/exception/404'),
    hidden: true
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
]

const createRouter = () => new Router({
  mode: 'history', // require service support,去掉Url中的#
  scrollBehavior: () => ({ y: 0 }),
  // base: process.env.BASE_URL,
  routes: constantRoutes
})
const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
